;; javascript and typescript config

(require 'init-package)

;; 设置使用 deno 提供 lsp 服务
;; (with-eval-after-load 'eglot
;;   (add-to-list 'eglot-server-programs
;;                '((js-mode
;;                   typescript-mode
;;                   (typescript-ts-base-mode :language-id "typescript"))
;;                  .
;;                  (eglot-deno "deno" "lsp")))

;;   (defclass eglot-deno (eglot-lsp-server) ()
;;     :documentation "A custom class for deno lsp.")

;;   (cl-defmethod eglot-initialization-options ((server eglot-deno))
;;     "Passes through required deno initialization options"
;;     (list :enable t
;;           :lint t)))

(add-hook 'js-base-mode-hook (lambda () (eglot-ensure)))

(if (treesit-available-p)
    (progn
      (setq-local js-source-alist
                  '((javascript . ("https://github.moeyy.xyz/https://github.com/tree-sitter/tree-sitter-javascript"))
                    (json       . ("https://github.moeyy.xyz/https://github.com/tree-sitter/tree-sitter-json"))
                    (typescript . ("https://github.moeyy.xyz/https://github.com/tree-sitter/tree-sitter-typescript" nil "typescript/src"))
                    (tsx        . ("https://github.moeyy.xyz/https://github.com/tree-sitter/tree-sitter-typescript" nil "tsx/src"))))
      (dolist (source js-source-alist)
        (push source treesit-language-source-alist)
        (unless (treesit-ready-p (car source))
          (treesit-install-language-grammar (car source))))

      (add-to-list 'auto-mode-alist '("\\.ts\\'"   . typescript-ts-mode))
      (add-to-list 'auto-mode-alist '("\\.tsx\\'"  . tsx-ts-mode))
      (add-to-list 'auto-mode-alist '("\\.js\\'"   . js-ts-mode))
      (add-to-list 'auto-mode-alist '("\\.jsx\\'"  . js-ts-mode))
      (add-to-list 'auto-mode-alist '("\\.json\\'" . json-ts-mode))
      (add-hook 'typescript-ts-base-mode-hook (lambda ()
                                                (eglot-ensure))))
  (progn
    (when (require-package 'typescript-mode)
      (with-eval-after-load 'typescript-mode
        (progn
          (add-hook typescript-mode-hook (lambda () (eglot-ensure))))))))

(provide 'init-js)
